import datetime

from peewee import *

import config
from config import DATABASE
import os

db = SqliteDatabase(DATABASE, pragmas=(('foreign_keys', 'on'),))


# 基本模型
class BaseModel(Model):
    class Meta:
        database = db


class Folder(BaseModel):
    name = CharField(max_length=64, unique=True)


# 分類
class Classify(BaseModel):
    folder = ForeignKeyField(Folder)
    name = CharField()
    img_url = CharField()


# 產品模型
class Product(BaseModel):
    folder = ForeignKeyField(Folder)  # 外鍵綁定对应目錄
    productName = CharField()
    product_id = IntegerField(primary_key=True, index=True)
    img_url = CharField()
    price = CharField()
    unit = CharField()
    amount = IntegerField
    region = CharField()
    village = CharField()
    seller_id = CharField()
    type = CharField()


# 用戶模型
class User(BaseModel):
    folder = ForeignKeyField(Folder)
    user_name= CharField(max_length=64, null=False)
    user_email_code= CharField(max_length=64, default='未知')
    user_gender= CharField(choices=('男','女'))
    user_qq= CharField(max_length=15, default='未知')
    user_education= CharField(max_length=10, default='未知')
    user_hobby= CharField(max_length=100, default='未知')
    user_introduce= CharField(default='未知')
    user_is_seller= BooleanField(default=False)
    create_time= DateTimeField(default=datetime.datetime.now)
    update_time= DateTimeField(default=datetime.datetime.now)
    user_openid = CharField(default="KingNoNeed")
    user_sessionKey = CharField(default="KingAlwaysPass")
    token= CharField()




def create_all_tables():  # 就賣家端还沒做呢，只能用本地的先生成個數据庫用着先
    db.connect()
    db.create_tables([Folder, Product, Classify, User])
    swiper = Folder.create(name="swiper")
    product = Folder.create(name="product")
    type = Folder.create(name="type")
    user = Folder.create(name="user")
    swiper.save()
    product.save()
    type.save()
    user.save()
    photoPath = config.UPLOAD_FOLDER + "\swiperImage"
    photoList = os.listdir(photoPath)
    with open(file=config.UPLOAD_FOLDER + "/productInfo/information.txt",
              mode="r",
              encoding="utf-8") as info:
        data = info.readlines()
    line = 0
    for i in range(0, len(data)):
        data[i] = data[i].strip()
    time = 0
    for i in photoList:
        product = Product.create(folder=data[line],
                                 productName=data[line + 1],
                                 product_id=int(data[line + 2]),
                                 img_url="/static/farm_shop_mall" + "/swiperImage/" + i,
                                 price=data[line + 4],
                                 unit=data[line + 5],
                                 amount=int(data[line + 6]),
                                 region=data[line + 7],
                                 village=data[line + 8],
                                 seller_id=data[line + 9],
                                 type=data[line + 10])
        product.save()
        time += 1
        if (time > 4):
            break
        line += 11
    typePath = config.UPLOAD_FOLDER + "/classifyImage"
    typeList = os.listdir(typePath)
    classifyList = ["瓜果蔬菜", "肉畜制品", "特色小吃", "手工制品"]
    for i in range(0, 4):
        type = Classify.create(folder=2, name=classifyList[i],
                               img_url="/static/farm_shop_mall" + "/classifyImage/" + typeList[i])
        type.save()
    productPath = config.UPLOAD_FOLDER + "\productImage"
    productList = os.listdir(productPath)
    with open(file=config.UPLOAD_FOLDER + "/productInfo/product_info.txt",
              mode="r",
              encoding="utf-8") as info:
        data = info.readlines()
    line = 0
    time = 0
    for i in range(0, len(data)):
        data[i] = data[i].strip()

    for i in productList:
        product = Product.create(folder=data[line],
                                 productName=data[line + 1],
                                 product_id=int(data[line + 2]),
                                 img_url="/static/farm_shop_mall" + "/productImage/" + i,
                                 price=data[line + 4],
                                 unit=data[line + 5],
                                 amount=int(data[line + 6]),
                                 region=data[line + 7],
                                 village=data[line + 8],
                                 seller_id=data[line + 9],
                                 type=data[line + 10])
        line += 11
        product.save()

    user = User.create(folder = 4,
                             user_email_code="a2064777923@gmail.com",
                             user_name="admin",
                             user_gender="男",
                             user_qq="2064777923",
                             user_education="本科",
                             user_is_seller=True,
                             token="adminIsTheKing")
    user.save()


if __name__ == '__main__':
    create_all_tables()
